<!-- components/StatCard.vue -->
<template>
  <el-card class="stat-card" :class="`stat-card--${type}`" :style="cardStyle">
    <div class="stat-card__content">
      <div class="stat-card__icon">
        <el-icon size="24">
          <component :is="iconComponent" />
        </el-icon>
      </div>
      <div class="stat-card__info">
        <div class="stat-card__title">{{ title }}</div>
        <div class="stat-card__value">
          {{ formattedValue }}
          <span v-if="suffix" class="stat-card__suffix">{{ suffix }}</span>
        </div>
      </div>
    </div>
    <div v-if="trend !== undefined" class="stat-card__trend" :class="trendClass">
      <el-icon>
        <ArrowUp v-if="trend > 0" />
        <ArrowDown v-else-if="trend < 0" />
        <Minus v-else />
      </el-icon>
      <span>{{ Math.abs(trend) }}%</span>
    </div>
    <div v-if="progress !== undefined" class="stat-card__progress">
      <el-progress 
        :percentage="progress" 
        :show-text="false" 
        :stroke-width="4"
        :color="progressColor"
      />
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { 
  SuccessFilled, WarningFilled, DataAnalysis, 
  MagicStick, Opportunity, ArrowUp, ArrowDown, Minus 
} from '@element-plus/icons-vue'

const props = defineProps<{
  title: string
  value?: number | string
  prefix?: string
  suffix?: string
  type?: 'primary' | 'success' | 'warning' | 'danger' | 'info'
  icon: string
  color?: string
  trend?: number
  progress?: number
}>()

const iconMap = {
  SuccessFilled,
  WarningFilled,
  DataAnalysis,
  MagicStick,
  Opportunity
}

const iconComponent = computed(() => {
  return iconMap[props.icon as keyof typeof iconMap] || SuccessFilled
})

const formattedValue = computed(() => {
  let result = ''
  if (props.prefix) {
    result += props.prefix
  }
  if (typeof props.value === 'number') {
    result += props.value.toLocaleString()
  } else {
    result += props.value
  }
  return result
})

const trendClass = computed(() => {
  if (props.trend === undefined) return ''
  return props.trend > 0 ? 'trend-up' : props.trend < 0 ? 'trend-down' : 'trend-neutral'
})

const progressColor = computed(() => {
  if (!props.progress) return '#409eff'
  if (props.progress >= 80) return '#67c23a'
  if (props.progress >= 60) return '#e6a23c'
  return '#f56c6c'
})

const cardStyle = computed(() => {
  if (props.color) {
    return {
      '--stat-card-color': props.color
    }
  }
  return {}
})
</script>

<style scoped>
.stat-card {
  height: 100%;
  transition: all 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.stat-card__content {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}

.stat-card__icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  background-color: var(--el-color-primary-light-9);
  color: var(--el-color-primary);
}

.stat-card--success .stat-card__icon {
  background-color: var(--el-color-success-light-9);
  color: var(--el-color-success);
}

.stat-card--warning .stat-card__icon {
  background-color: var(--el-color-warning-light-9);
  color: var(--el-color-warning);
}

.stat-card--danger .stat-card__icon {
  background-color: var(--el-color-danger-light-9);
  color: var(--el-color-danger);
}

.stat-card--info .stat-card__icon {
  background-color: var(--el-color-info-light-9);
  color: var(--el-color-info);
}

.stat-card__info {
  flex: 1;
}

.stat-card__title {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin-bottom: 4px;
}

.stat-card__value {
  font-size: 20px;
  font-weight: 600;
  color: var(--el-text-color-primary);
}

.stat-card__suffix {
  font-size: 14px;
  font-weight: normal;
  margin-left: 4px;
}

.stat-card__trend {
  display: flex;
  align-items: center;
  font-size: 12px;
  font-weight: 500;
}

.trend-up {
  color: var(--el-color-success);
}

.trend-down {
  color: var(--el-color-danger);
}

.trend-neutral {
  color: var(--el-color-info);
}

.stat-card__progress {
  margin-top: 8px;
}
</style>